package main

import "fmt"

type ListNode struct {
	Val  int
	Next *ListNode
}

//双指针:0ms 100%, 2.7MB 59%
func deleteNode(head *ListNode, val int) *ListNode {
	if head.Val == val {
		return head.Next
	}
	for pre, cur := head, head.Next; pre.Next != nil; {
		if cur.Val == val {
			pre.Next = cur.Next
			break
		} else {
			pre = pre.Next
			cur = cur.Next
		}
	}
	return head
}

//单指针:4ms 52%, 2.7MB 100%
func deleteNode1(head *ListNode, val int) *ListNode {
	if head.Val == val {
		return head.Next
	}
	for pre := head; pre.Next != nil; {
		if pre.Next.Val == val {
			pre.Next = pre.Next.Next
			break
		} else {
			pre = pre.Next
		}
	}
	return head
}

func main() {
	lNode := &ListNode{1, &ListNode{2, nil}}
	fmt.Println(deleteNode(lNode, 2))
	fmt.Println(deleteNode1(lNode, 2))
}
